#include <assert.h>
#include "math.h"
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
/* widgets:
Layer_TouchTest
Background_TouchTest
Container_GestureTest
Icon_GestureIndicate
Animation_TurnDown
Icon51
Icon50
Icon49
Animation_TurnUp
Icon48
Icon47
Icon46
Animation_TurnRight
Icon45
Icon44
Icon43
Animation_TurnLeft
Icon42
Icon41
Icon40
Container_ColorTest
Icon_ColorIndicate
Button_ColorTestBlue
Button_ColorTestGreen
Button_ColorTestRed
Container_SlideTest
Icon_SlideIndicate
Meter_SlideTest_Xuanzhuan
Icon_SlideTest_Pointer
CircleProgressBar_SlideTest
Icon_SlideTest_Jingdu
Container_PopupButtonTest
Icon_TouchIndicate
Container_IconPass
Icon_TestBig8
Icon_TestBig7
Icon_TestBig6
Icon_TestBig5
Icon_TestBig4
Icon_TestBig3
Icon_TestBig2
Icon_TestBig1
Icon_Test5
Icon_Test4
Icon_Test3
Icon_Test2
Icon_Test1
PopupButton_TestBig8
PopupButton_TestBig7
PopupButton_TestBig6
PopupButton_TestBig5
PopupButton_TestBig4
PopupButton_TestBig3
PopupButton_TestBig2
PopupButton_TestBig1
PopupButton_Test5
PopupButton_Test4
PopupButton_Test3
PopupButton_Test2
PopupButton_Test1
*/
void Gesture_Animatin_Stop(void);

static ITUBackground* Background_TouchTest;
static ITUContainer* Container_PopupButtonTest;
static ITUContainer* Container_SlideTest;
static ITUContainer* Container_ColorTest;
static ITUContainer* Container_GestureTest;
static ITUContainer* Container_TestPass;
static ITUMeter* Meter_SlideTest_Xuanzhuan;
static ITUAnimation* Animation_TurnLeft, *Animation_TurnRight, *Animation_TurnUp, *Animation_TurnDown;
static ITUAnimation* Animation_TestPass;
static ITULayer* Layer_BootLogo;
bool TouchTestOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_TouchTest)
	{
		Background_TouchTest = ituSceneFindWidget(&theScene, "Background_TouchTest");
		assert(Background_TouchTest);
		Container_PopupButtonTest = ituSceneFindWidget(&theScene, "Container_PopupButtonTest");
		assert(Container_PopupButtonTest);
		Container_SlideTest = ituSceneFindWidget(&theScene, "Container_SlideTest");
		assert(Container_SlideTest);
		Container_ColorTest = ituSceneFindWidget(&theScene, "Container_ColorTest");
		assert(Container_ColorTest);
		Container_GestureTest = ituSceneFindWidget(&theScene, "Container_GestureTest");
		assert(Container_GestureTest);
		Container_TestPass = ituSceneFindWidget(&theScene, "Container_TestPass");
		assert(Container_TestPass);
		Meter_SlideTest_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_SlideTest_Xuanzhuan");
		assert(Meter_SlideTest_Xuanzhuan);
		Animation_TurnLeft = ituSceneFindWidget(&theScene, "Animation_TurnLeft");
		assert(Animation_TurnLeft);
		Animation_TurnRight = ituSceneFindWidget(&theScene, "Animation_TurnRight");
		assert(Animation_TurnRight);
		Animation_TurnUp = ituSceneFindWidget(&theScene, "Animation_TurnUp");
		assert(Animation_TurnUp);
		Animation_TurnDown = ituSceneFindWidget(&theScene, "Animation_TurnDown");
		assert(Animation_TurnDown);
		Animation_TestPass = ituSceneFindWidget(&theScene, "Animation_TestPass");
		assert(Animation_TestPass);
	}
	ituAnimationSetOnStop(Animation_TurnRight, Gesture_Animatin_Stop);
	ituAnimationSetOnStop(Animation_TurnDown, Gesture_Animatin_Stop);
	ituAnimationSetOnStop(Animation_TurnUp, Gesture_Animatin_Stop);
	ituAnimationSetOnStop(Animation_TurnLeft, Gesture_Animatin_Stop);
    return true;
}
static uint8_t ChackRet[11];
bool TouchTestOnTimer(ITUWidget* widget, char* param)
{
	uint16_t TempValue;
	uint8_t i;

	if (ituWidgetIsVisible(Container_SlideTest))//滑动检测
	{
		TempValue = Meter_SlideTest_Xuanzhuan->value;
		ChackRet[TempValue / 34] = 1;
		for (i = 0; i < 10; i++)
		{
			if (ChackRet[i] == 0)
			{
				break;
			}
			else
			{
				if (i == 9 && TempValue>335)
				{
					ituWidgetSetVisible(Container_SlideTest, false);//设置为隐藏
					ituWidgetSetVisible(Container_ColorTest, true);//设置为隐藏
					memset(ChackRet, 0, sizeof(ChackRet));
				}
			}
		}
	}
	else if (ituWidgetIsVisible(Container_GestureTest))//手势检测
	{
		if (Action_Cmd.User_Action == Left_Slide)//向左
		{
			ituAnimationReset(Animation_TurnLeft);
			ituAnimationPlay(Animation_TurnLeft, 0);
			Clear_User_Action();
			ChackRet[0] = 1;
		}
		else if (Action_Cmd.User_Action == Right_Slide)//向右
		{
			ituAnimationReset(Animation_TurnRight);
			ituAnimationPlay(Animation_TurnRight, 0);
			Clear_User_Action();
			ChackRet[1] = 1;
		}
		else if (Action_Cmd.User_Action == Up_Slide)//向上
		{
			ituAnimationReset(Animation_TurnUp);
			ituAnimationPlay(Animation_TurnUp, 0);
			Clear_User_Action();
			ChackRet[2] = 1;
		}
		else if (Action_Cmd.User_Action == Down_Slide)//向下
		{
			ituAnimationReset(Animation_TurnDown);
			ituAnimationPlay(Animation_TurnDown, 0);
			Clear_User_Action();
			ChackRet[3] = 1;
		}
	}
    return true;
}

bool Meter_SlideTest_Xuanzhuan_Changed(ITUWidget* widget, char* param)
{
    return true;
}

bool Touch_Position_Test(ITUWidget* widget, char* param)
{
	static uint8_t PointNum = 0;
	PointNum++;
	if (PointNum == 13)//总点击数
	{
		ituWidgetSetVisible(Container_PopupButtonTest, false);//设置为隐藏
		ituWidgetSetVisible(Container_SlideTest, true);//设置为隐藏
	}
    return true;
}
void Gesture_Animatin_Stop(void)
{
	uint8_t i;
	for (i = 0; i < 4; i++)
	{
		if (ChackRet[i] == 0)
		{
			break;
		}
		else
		{
			if (i == 3)
			{
				ituWidgetSetVisible(Container_GestureTest, false);//设置为隐藏
				ituWidgetSetVisible(Container_TestPass, true);//设置为隐藏
				ituAnimationReset(Animation_TestPass);
				ituAnimationPlay(Animation_TestPass, 0);
				//ituLayerGoto(Layer_BootLogo);
			}
		}
	}
}
